package com.calendar.month;

import java.util.ArrayList;
import java.util.Calendar;

import com.calendar.add.AddView;
import com.calendar.backend.Help;
import com.calendar.day.DayView;
import com.calendar.widget.R;
import android.app.Activity;
import android.content.Intent;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class CalendarView extends Activity implements Button.OnClickListener,
OnGesturePerformedListener {

	public static final int MENU_DAY = Menu.FIRST + 1;
	public static final int MENU_SETTINGS = Menu.FIRST + 2;
	public static final int MENU_EXIT = Menu.FIRST + 3;
	public static final int MENU_HELP = Menu.FIRST + 4;


	int dayFinal;
	int monthLength;

	Button one;
	Button two;
	Button three;
	Button four;
	Button five;
	Button six;
	Button seven;
	Button eight;
	Button nine;
	Button ten;
	Button eleven;
	Button twelve;
	Button thirteen;
	Button fourteen;
	Button fifteen;
	Button sixteen;
	Button seventeen;
	Button eighteen;
	Button nineteen;
	Button twenty;
	Button twentyone;
	Button twentytwo;
	Button twentythree;
	Button twentyfour;
	Button twentyfive;
	Button twentysix;
	Button twentyseven;
	Button twentyeight;
	Button twentynine;
	Button thirty;
	Button thirtyone;
	Button thirtytwo;
	Button thirtythree;
	Button thirtyfour;
	Button thirtyfive;
	Button thirtysix;
	Button thirtyseven;
	Button thirtyeight;
	Button thirtynine;
	Button fourty;
	Button fourtyone;
	Button fourtytwo;

	TextView monthyear;

	Calendar now;

	int month;
	int year;
	int day;
	int[] monthTable = new int[12];
	String[] months = new String[12];
	boolean leapYear;
	Button[][] calendar = new Button[6][7];

	GestureLibrary mLibrary;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.month_view);

		one = (Button) findViewById(R.id.one);
		two = (Button) findViewById(R.id.two);
		three = (Button) findViewById(R.id.three);
		four = (Button) findViewById(R.id.four);
		five = (Button) findViewById(R.id.five);
		six = (Button) findViewById(R.id.six);
		seven = (Button) findViewById(R.id.seven);
		eight = (Button) findViewById(R.id.eight);
		nine = (Button) findViewById(R.id.nine);
		ten = (Button) findViewById(R.id.ten);
		eleven = (Button) findViewById(R.id.eleven);
		twelve = (Button) findViewById(R.id.twelve);
		thirteen = (Button) findViewById(R.id.thirteen);
		fourteen = (Button) findViewById(R.id.fourteen);
		fifteen = (Button) findViewById(R.id.fifteen);
		sixteen = (Button) findViewById(R.id.sixteen);
		seventeen = (Button) findViewById(R.id.seventeen);
		eighteen = (Button) findViewById(R.id.eighteen);
		nineteen = (Button) findViewById(R.id.nineteen);
		twenty = (Button) findViewById(R.id.twenty);
		twentyone = (Button) findViewById(R.id.twentyone);
		twentytwo = (Button) findViewById(R.id.twentytwo);
		twentythree = (Button) findViewById(R.id.twentythree);
		twentyfour = (Button) findViewById(R.id.twentyfour);
		twentyfive = (Button) findViewById(R.id.twentyfive);
		twentysix = (Button) findViewById(R.id.twentysix);
		twentyseven = (Button) findViewById(R.id.twentyseven);
		twentyeight = (Button) findViewById(R.id.twentyeight);
		twentynine = (Button) findViewById(R.id.twentynine);
		thirty = (Button) findViewById(R.id.thirty);
		thirtyone = (Button) findViewById(R.id.thirtyone);
		thirtytwo = (Button) findViewById(R.id.thirtytwo);
		thirtythree = (Button) findViewById(R.id.thirtythree);
		thirtyfour = (Button) findViewById(R.id.thirtyfour);
		thirtyfive = (Button) findViewById(R.id.thirtyfive);
		thirtysix = (Button) findViewById(R.id.thirtysix);
		thirtyseven = (Button) findViewById(R.id.thirtyseven);
		thirtyeight = (Button) findViewById(R.id.thirtyeight);
		thirtynine = (Button) findViewById(R.id.thirtynine);
		fourty = (Button) findViewById(R.id.fourty);
		fourtyone = (Button) findViewById(R.id.fourtyone);
		fourtytwo = (Button) findViewById(R.id.fourtytwo);

		calendar[0][0] = one;
		calendar[0][1] = two;
		calendar[0][2] = three;
		calendar[0][3] = four;
		calendar[0][4] = five;
		calendar[0][5] = six;
		calendar[0][6] = seven;
		calendar[1][0] = eight;
		calendar[1][1] = nine;
		calendar[1][2] = ten;
		calendar[1][3] = eleven;
		calendar[1][4] = twelve;
		calendar[1][5] = thirteen;
		calendar[1][6] = fourteen;
		calendar[2][0] = fifteen;
		calendar[2][1] = sixteen;
		calendar[2][2] = seventeen;
		calendar[2][3] = eighteen;
		calendar[2][4] = nineteen;
		calendar[2][5] = twenty;
		calendar[2][6] = twentyone;
		calendar[3][0] = twentytwo;
		calendar[3][1] = twentythree;
		calendar[3][2] = twentyfour;
		calendar[3][3] = twentyfive;
		calendar[3][4] = twentysix;
		calendar[3][5] = twentyseven;
		calendar[3][6] = twentyeight;
		calendar[4][0] = twentynine;
		calendar[4][1] = thirty;
		calendar[4][2] = thirtyone;
		calendar[4][3] = thirtytwo;
		calendar[4][4] = thirtythree;
		calendar[4][5] = thirtyfour;
		calendar[4][6] = thirtyfive;
		calendar[5][0] = thirtysix;
		calendar[5][1] = thirtyseven;
		calendar[5][2] = thirtyeight;
		calendar[5][3] = thirtynine;
		calendar[5][4] = fourty;
		calendar[5][5] = fourtyone;
		calendar[5][6] = fourtytwo;

		for (int i = 0; i < 6; i++) {
			for (int j = 0; j < 7; j++) {
				calendar[i][j].setOnClickListener(this);
			}
		}


		monthyear = (TextView) findViewById(R.id.monthyear);

		months[0] = "January";
		months[1] = "February";
		months[2] = "March";
		months[3] = "April";
		months[4] = "May";
		months[5] = "June";
		months[6] = "July";
		months[7] = "August";
		months[8] = "September";
		months[9] = "October";
		months[10] = "November";
		months[11] = "December";

		monthTable[0] = 0;
		monthTable[1] = 3;
		monthTable[2] = 3;
		monthTable[3] = 6;
		monthTable[4] = 1;
		monthTable[5] = 4;
		monthTable[6] = 6;
		monthTable[7] = 2;
		monthTable[8] = 5;
		monthTable[9] = 0;
		monthTable[10] = 3;
		monthTable[11] = 5;

		now = Calendar.getInstance();

		month = now.get(Calendar.MONTH);
		year = now.get(Calendar.YEAR);
		day = now.get(Calendar.DAY_OF_MONTH);

		GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
		gestures.addOnGesturePerformedListener(this);

		mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
		if (!mLibrary.load()) {
			finish();
		}

		// Render current month
		renderMonth();
	}


	public void onClick(View view) {
		// When a day is clicked start day view
		if (view == calendar[0][0]) {
			newActivity(0 - dayFinal + 1);
		} else if (view == calendar[0][1]) {
			newActivity(1 - dayFinal + 1);
		} else if (view == calendar[0][2]) {
			newActivity(2 - dayFinal + 1);
		} else if (view == calendar[0][3]) {
			newActivity(3 - dayFinal + 1);
		} else if (view == calendar[0][4]) {
			newActivity(4 - dayFinal + 1);
		} else if (view == calendar[0][5]) {
			newActivity(5 - dayFinal + 1);
		} else if (view == calendar[0][6]) {
			newActivity(6 - dayFinal + 1);
		} else if (view == calendar[1][0]) {
			newActivity(7 - dayFinal + 1);
		} else if (view == calendar[1][1]) {
			newActivity(8 - dayFinal + 1);
		} else if (view == calendar[1][2]) {
			newActivity(9 - dayFinal + 1);
		} else if (view == calendar[1][3]) {
			newActivity(10 - dayFinal + 1);
		} else if (view == calendar[1][4]) {
			newActivity(11 - dayFinal + 1);
		} else if (view == calendar[1][5]) {
			newActivity(12 - dayFinal + 1);
		} else if (view == calendar[1][6]) {
			newActivity(13 - dayFinal + 1);
		} else if (view == calendar[2][0]) {
			newActivity(14 - dayFinal + 1);
		} else if (view == calendar[2][1]) {
			newActivity(15 - dayFinal + 1);
		} else if (view == calendar[2][2]) {
			newActivity(16 - dayFinal + 1);
		} else if (view == calendar[2][3]) {
			newActivity(17 - dayFinal + 1);
		} else if (view == calendar[2][4]) {
			newActivity(18 - dayFinal + 1);
		} else if (view == calendar[2][5]) {
			newActivity(19 - dayFinal + 1);
		} else if (view == calendar[2][6]) {
			newActivity(20 - dayFinal + 1);
		} else if (view == calendar[3][0]) {
			newActivity(21 - dayFinal + 1);
		} else if (view == calendar[3][1]) {
			newActivity(22 - dayFinal + 1);
		} else if (view == calendar[3][2]) {
			newActivity(23 - dayFinal + 1);
		} else if (view == calendar[3][3]) {
			newActivity(24 - dayFinal + 1);
		} else if (view == calendar[3][4]) {
			newActivity(25 - dayFinal + 1);
		} else if (view == calendar[3][5]) {
			newActivity(26 - dayFinal + 1);
		} else if (view == calendar[3][6]) {
			newActivity(27 - dayFinal + 1);
		} else if (view == calendar[4][0]) {
			newActivity(28 - dayFinal + 1);
		} else if (view == calendar[4][1]) {
			newActivity(29 - dayFinal + 1);
		} else if (view == calendar[4][2]) {
			newActivity(30 - dayFinal + 1);
		} else if (view == calendar[4][3]) {
			newActivity(31 - dayFinal + 1);
		} else if (view == calendar[4][4]) {
			newActivity(32 - dayFinal + 1);
		} else if (view == calendar[4][5]) {
			newActivity(33 - dayFinal + 1);
		} else if (view == calendar[4][6]) {
			newActivity(34 - dayFinal + 1);
		} else if (view == calendar[5][0]) {
			newActivity(35 - dayFinal + 1);
		} else if (view == calendar[5][1]) {
			newActivity(36 - dayFinal + 1);
		} else if (view == calendar[5][2]) {
			newActivity(37 - dayFinal + 1);
		} else if (view == calendar[5][3]) {
			newActivity(38 - dayFinal + 1);
		} else if (view == calendar[5][4]) {
			newActivity(39 - dayFinal + 1);
		} else if (view == calendar[5][5]) {
			newActivity(40 - dayFinal + 1);
		} else if (view == calendar[5][6]) {
			newActivity(41 - dayFinal + 1);
		}
	}

	public void onBackPressed(){
		this.finish();
	}

	// Goes to the dayView from this view
	public void newActivity(int key) {
		Intent myIntent = new Intent(getBaseContext(), DayView.class);
		myIntent.putExtra("day", key);
		myIntent.putExtra("month", month);
		myIntent.putExtra("year", year);
		startActivityForResult(myIntent, 0);
		this.finish();
	}

	// Renders the month every time you swipe one way or another
	public void renderMonth() {
		// Figures out what day the month starts on
		int lastDigit = year % 2000;
		monthyear.setText(months[month] + " " + year);
		if (lastDigit % 4 == 0) {
			leapYear = true;
		}

		int century = 6;
		int monthVal = monthTable[month];
		int total = century + monthVal + lastDigit + (lastDigit % 4);
		dayFinal = total % 7;

		int dayNum = 1;
		monthLength = decideMonthTotal(leapYear);
		// Clears the old data from calendar
		clearCalendar();
		if (monthLength + dayFinal > 35) {
			for (int i = 0; i < 7; i++) {
				calendar[5][i].setVisibility(0);
				calendar[5][i].setClickable(false);
			}
		} else {
			for (int i = 0; i < 7; i++) {
				calendar[5][i].setVisibility(100);
				calendar[5][i].setClickable(true);
			}
		}
		boolean breaker = false;
		// Writes new numbers to calendar
		int temp1 = 0;
		int temp2 = 0;
		int temp3 = 0;
		for (int i = 0; i < 6; i++) {
			if (breaker) {
				break;
			}
			for (int j = 0; j < 7; j++) {
				if(j < dayFinal && i == 0){

				}
				else{
					if(dayNum == day){
						temp1 = i;
						temp2 = j;
						temp3 = dayNum;
					}
					setPic(calendar[i][j], dayNum);
					calendar[i][j].setClickable(true);
					dayNum++;
					if (monthLength < dayNum) {
						//dayFinal = 0;
						breaker = true;
						break;
					}
				}
			}
			//dayFinal = 0;
		}
		Calendar cal;
		cal = Calendar.getInstance();
		if(month == cal.get(Calendar.MONTH)){
			setToday(calendar[temp1][temp2], temp3);
		}
	}

	public void setToday(Button but, int i){
		if(i == 1){
			but.setBackgroundResource(R.drawable.oney);
		}
		else if(i == 2){
			but.setBackgroundResource(R.drawable.twoy);
		}
		else if(i == 3){
			but.setBackgroundResource(R.drawable.threey);
		}else if(i == 4){
			but.setBackgroundResource(R.drawable.foury);
		}else if(i == 5){
			but.setBackgroundResource(R.drawable.fivey);
		}else if(i == 6){
			but.setBackgroundResource(R.drawable.sixy);
		}else if(i == 7){
			but.setBackgroundResource(R.drawable.seveny);
		}else if(i == 8){
			but.setBackgroundResource(R.drawable.eighty);
		}else if(i == 9){
			but.setBackgroundResource(R.drawable.niney);
		}else if(i == 10){
			but.setBackgroundResource(R.drawable.teny);
		}else if(i == 11){
			but.setBackgroundResource(R.drawable.eleveny);
		}else if(i == 12){
			but.setBackgroundResource(R.drawable.twelvey);
		}else if(i == 13){
			but.setBackgroundResource(R.drawable.thirteeny);
		}else if(i == 14){
			but.setBackgroundResource(R.drawable.fourteeny);
		}else if(i == 15){
			but.setBackgroundResource(R.drawable.fifteeny);
		}else if(i == 16){
			but.setBackgroundResource(R.drawable.sixteeny);
		}else if(i == 17){
			but.setBackgroundResource(R.drawable.seventeeny);
		}else if(i == 18){
			but.setBackgroundResource(R.drawable.eighteeny);
		}else if(i == 19){
			but.setBackgroundResource(R.drawable.nineteeny);
		}else if(i == 20){
			but.setBackgroundResource(R.drawable.twentyy);
		}else if(i == 21){
			but.setBackgroundResource(R.drawable.twentyoney);
		}else if(i == 22){
			but.setBackgroundResource(R.drawable.twentytwoy);
		}else if(i == 23){
			but.setBackgroundResource(R.drawable.twentythreey);
		}else if(i == 24){
			but.setBackgroundResource(R.drawable.twentyfoury);
		}else if(i == 25){
			but.setBackgroundResource(R.drawable.twentyfivey);
		}else if(i == 26){
			but.setBackgroundResource(R.drawable.twentysixy);
		}else if(i == 27){
			but.setBackgroundResource(R.drawable.twentyseveny);
		}else if(i == 28){
			but.setBackgroundResource(R.drawable.twentyeighty);
		}else if(i == 29){
			but.setBackgroundResource(R.drawable.twentyniney);
		}else if(i == 30){
			but.setBackgroundResource(R.drawable.thirtyy);
		}else if(i == 31){
			but.setBackgroundResource(R.drawable.thirtyoney);
		}
	}

	public void setPic(Button but, int i){
		if(i == 1){
			but.setBackgroundResource(R.drawable.oneg);
		}
		else if(i == 2){
			but.setBackgroundResource(R.drawable.twog);
		}
		else if(i == 3){
			but.setBackgroundResource(R.drawable.threeg);
		}else if(i == 4){
			but.setBackgroundResource(R.drawable.fourg);
		}else if(i == 5){
			but.setBackgroundResource(R.drawable.fiveg);
		}else if(i == 6){
			but.setBackgroundResource(R.drawable.sixg);
		}else if(i == 7){
			but.setBackgroundResource(R.drawable.seveng);
		}else if(i == 8){
			but.setBackgroundResource(R.drawable.eightg);
		}else if(i == 9){
			but.setBackgroundResource(R.drawable.nineg);
		}else if(i == 10){
			but.setBackgroundResource(R.drawable.teng);
		}else if(i == 11){
			but.setBackgroundResource(R.drawable.eleveng);
		}else if(i == 12){
			but.setBackgroundResource(R.drawable.twelveg);
		}else if(i == 13){
			but.setBackgroundResource(R.drawable.thirteeng);
		}else if(i == 14){
			but.setBackgroundResource(R.drawable.fourteeng);
		}else if(i == 15){
			but.setBackgroundResource(R.drawable.fifteeng);
		}else if(i == 16){
			but.setBackgroundResource(R.drawable.sixteeng);
		}else if(i == 17){
			but.setBackgroundResource(R.drawable.seventeeng);
		}else if(i == 18){
			but.setBackgroundResource(R.drawable.eighteeng);
		}else if(i == 19){
			but.setBackgroundResource(R.drawable.nineteeng);
		}else if(i == 20){
			but.setBackgroundResource(R.drawable.twentyg);
		}else if(i == 21){
			but.setBackgroundResource(R.drawable.twentyoneg);
		}else if(i == 22){
			but.setBackgroundResource(R.drawable.twentytwog);
		}else if(i == 23){
			but.setBackgroundResource(R.drawable.twentythreeg);
		}else if(i == 24){
			but.setBackgroundResource(R.drawable.twentyfourg);
		}else if(i == 25){
			but.setBackgroundResource(R.drawable.twentyfiveg);
		}else if(i == 26){
			but.setBackgroundResource(R.drawable.twentysixg);
		}else if(i == 27){
			but.setBackgroundResource(R.drawable.twentyseveng);
		}else if(i == 28){
			but.setBackgroundResource(R.drawable.twentyeightg);
		}else if(i == 29){
			but.setBackgroundResource(R.drawable.twentynineg);
		}else if(i == 30){
			but.setBackgroundResource(R.drawable.thirtyg);
		}else if(i == 31){
			but.setBackgroundResource(R.drawable.thirtyoneg);
		}
	}

	// Erases calendar
	public void clearCalendar() {
		for (int i = 0; i < 6; i++) {
			for (int j = 0; j < 7; j++) {
				calendar[i][j].setBackgroundResource(R.drawable.grey);
				calendar[i][j].setClickable(false);
			}
		}
	}

	// Decide the lenght of the month
	public int decideMonthTotal(boolean leap) {
		if (month == 0 || month == 2 || month == 4 || month == 6 || month == 7
				|| month == 9 || month == 11) {
			return 31;
		} else if (month == 1 && leap == true) {
			return 29;
		} else if (month == 1 && leap == false) {
			return 28;
		} else {
			return 30;
		}
	}

	public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
		ArrayList<Prediction> predictions = mLibrary.recognize(gesture);

		// We want at least one prediction
		if (predictions.size() > 0 && predictions.get(0).score > 1.0
				&& gesture.getStrokesCount() > 1) {
			String what = "";
			for (int i = 0; i < predictions.size(); i++) {
				if (predictions.get(i).name.equals("Add1")
						|| predictions.get(i).name.equals("Add2")
						|| predictions.get(i).name.equals("Add3")
						|| predictions.get(i).name.equals("Add4")
						|| predictions.get(i).name.equals("Add5")
						|| predictions.get(i).name.equals("Add6")
						|| predictions.get(i).name.equals("Add7")
						|| predictions.get(i).name.equals("Add8")
						|| predictions.get(i).name.equals("Agenda")) {
					what = predictions.get(i).name;
					break;
				}
			}
			if (what.equals("Agenda")) {
				newActivity(day);
			} else if (what.equals("Add1") || what.equals("Add2")
					|| what.equals("Add3") || what.equals("Add4")
					|| what.equals("Add5") || what.equals("Add6")
					|| what.equals("Add7") || what.equals("Add8")) {
				Intent myIntent = new Intent(getBaseContext(), AddView.class);
				myIntent.setAction("Add" + Math.random());
				myIntent.putExtra("day", day);
				myIntent.putExtra("month", month);
				myIntent.putExtra("year", year);
				startActivityForResult(myIntent, 0);
			}
			Toast.makeText(this, what, Toast.LENGTH_SHORT).show();
		} else if (predictions.size() > 0 && predictions.get(0).score > 1.0
				&& gesture.getStrokesCount() == 1) {

			String what = "";
			for (int i = 0; i < predictions.size(); i++) {
				if (predictions.get(i).name.equals("Next")
						|| predictions.get(i).name.equals("Previous")
						|| predictions.get(i).name.equals("Help")
						|| predictions.get(i).name.equals("Month2")
						|| predictions.get(i).name.equals("Month")) {
					what = predictions.get(i).name;
					break;
				}
			}
			Toast.makeText(this, what, Toast.LENGTH_SHORT).show();
			if (what.equals("Next")) {
				month++;
				if (month == 12) {
					year++;
					month = 0;
				}
				renderMonth();
			} else if (what.equals("Previous")) {
				month--;
				if (month == -1) {
					year--;
					month = 11;
				}
				renderMonth();
			} else if (what.equals("Help")) {
				Intent myIntent = new Intent(getBaseContext(), Help.class);
				startActivityForResult(myIntent, 0);
			}
		}
	}

	/* Creates the menu items */
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, MENU_DAY, 0, "Day").setIcon(
				android.R.drawable.ic_menu_day);
		// menu.add(0, MENU_SETTINGS, 0, "Settings").setIcon(
		// android.R.drawable.ic_menu_preferences);
		menu.add(0, MENU_SETTINGS, 0, "Settings").setIcon(
				android.R.drawable.ic_menu_preferences);
		menu.add(0, MENU_HELP, 0, "Help").setIcon(
				android.R.drawable.ic_menu_help);
		menu.add(0, MENU_EXIT, 0, "Exit").setIcon(
				android.R.drawable.ic_menu_close_clear_cancel);
		return true;
	}

	/* Handles item selections */
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent myIntent;
		switch (item.getItemId()) {
		case MENU_DAY:
			newActivity(day);
			return true;
		case MENU_SETTINGS:

			return true;
		case MENU_HELP:
			myIntent = new Intent(getBaseContext(), Help.class);
			startActivityForResult(myIntent, 0);
			return true;

		case MENU_EXIT:

			return true;
		}
		return false;
	}
}